<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="标签模板编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'labelTemplateNo', validatorRules.labelTemplateNo]" placeholder="请输入标签模板编号"></p-input>
        </p-form-item>
        <p-form-item label="标签模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'labelTemplateName', validatorRules.labelTemplateName]" placeholder="请输入标签模板名称"></p-input>
        </p-form-item>
        <p-form-item label="标签模板尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'labelTemplateLength', validatorRules.labelTemplateLength]" placeholder="请输入标签模板尺寸"></p-input>
        </p-form-item>
        <p-form-item label="标签模板描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'labelTemplateDescrtion', validatorRules.labelTemplateDescrtion]" placeholder="请输入标签模板描述"></p-input>
        </p-form-item>
        <p-form-item label="标签类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['labelType', validatorRules.labelType]" :trigger-change="true" dictCode="mes_label_type" placeholder="请选择标签类型"/>
        </p-form-item>
        <p-form-item label="责任部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-depart v-decorator="['dptname', validatorRules.dptname]" :trigger-change="true"/>
        </p-form-item>
        <p-form-item label="标签纸颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['labelColor', validatorRules.labelColor]" :trigger-change="true" dictCode="mes_label_color" placeholder="请选择标签纸颜色"/>
        </p-form-item>
        <p-form-item label="碳带型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'ribbonsType', validatorRules.ribbonsType]" placeholder="请输入碳带型号"></p-input>
        </p-form-item>
        <p-form-item label="打印机型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['printerType', validatorRules.printerType]" :trigger-change="true" dictCode="mes_printer_type" placeholder="请选择打印机型号"/>
        </p-form-item>
        <p-form-item label="是否包含流水号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['containSn', validatorRules.containSn]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否包含流水号"/>
        </p-form-item>
        <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'status', validatorRules.status]" placeholder="请输入状态"></p-input>
        </p-form-item>
        <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction, putAction, api } from '@/api/manage'
  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MesBcTemplateModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JSelectDepart,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          labelTemplateNo: {rules: [
            {required: true, message: '请输入标签模板编号!'},
          ]},
          labelTemplateName: {rules: [
            {required: true, message: '请输入标签模板名称!'},
          ]},
          labelTemplateLength: {rules: [
            {required: true, message: '请输入标签模板尺寸!'},
          ]},
          labelTemplateDescrtion: {rules: [
          ]},
          labelType: {rules: [
          ]},
          dptname: {rules: [
          ]},
          labelColor: {rules: [
            {required: true, message: '请输入标签纸颜色!'},
          ]},
          ribbonsType: {rules: [
          ]},
          printerType: {rules: [
            {required: true, message: '请输入打印机型号!'},
          ]},
          containSn: {rules: [
            {required: true, message: '请输入是否包含流水号!'},
          ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
          remark: {rules: [
          ]},
        },
        url: {
          add: '/mes-code/auto/mesBcTemplate/add',
          edit: '/mes-code/auto/mesBcTemplate/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },

      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_label_template_no", "columnName": "labelTemplateNo"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','labelTemplateNo','labelTemplateName','labelTemplateLength','labelTemplateDescrtion','labelType','dptname','labelColor','ribbonsType','printerType','containSn','status','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','labelTemplateNo','labelTemplateName','labelTemplateLength','labelTemplateDescrtion','labelType','dptname','labelColor','ribbonsType','printerType','containSn','status','remark'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>